/**
 * 报销人工号
 */
var financePsnCode = _Z.tb0.financeworkcode;
/**
 * 报销人所属财务公司编码
 */
var financeUnitCode = _Z.tb0.financeunitcode;
/**
 * 冲销原币金额
 */
var chongxybje = _Z.tb0.chongxybje;
/**
 * 报销合计金额
 */
var hejje = _Z.tb0.hejje;

//历史借款明细表
/**
 * NC借款单主键
 */
var loanPk = _Z.tb2.loanpk;
/**
 * 借款单号
 */
var loanCode = _Z.tb2.jiekdh;
/**
 * 借款日期
 */
var loanDate = _Z.tb2.loandate;
/**
 * 借款人
 */
var loanPsn = _Z.tb2.loanpsnname;
/**
 * 借款公司
 */
var loanPsnCom = _Z.tb2.loanpsncom;
/**
 * 借款部门
 */
var loanPsnDept = _Z.tb2.loanpsndept;
/**
 * 借款事由
 */
var loanNote = _Z.tb2.loannote;
/**
 * 借款金额
 */
var loanAmount = _Z.tb2.loanamount;
/**
 * 借款余额
 */
var loanBalance = _Z.tb2.loanbalance;
/**
 * 冲销金额
 */
var reversalAmount = _Z.tb2.chongxybje;

jQuery(document).ready(function(){
	_C.bindChange(financePsnCode,function(){
		setHistoryLoan();
	});
	_C.inputEach(reversalAmount,function(n,i,value){
		_C.bindChange(reversalAmount+n,function(){
			var loanBalanceId = loanBalance+n;
			var reversalAmountId = reversalAmount+n;
			checkReversalAmounts(loanBalanceId,reversalAmountId);
		});
	});
	var fn_checkCustomize = window.checkCustomize;
	window.checkCustomize = function(){
		if(fn_checkCustomize()){
			let chongxybjeVal = _F.getNum(jQuery("#"+chongxybje).val());
			let hejjeVal = _F.getNum(jQuery("#"+hejje).val());
			if(chongxybjeVal > hejjeVal){
				window.top.Dialog.alert("冲销原币金额不能大于报销合计金额!");
				return false;
			}
			return true;
		}
		return false;
	};
});

/**
 * 设置历史借款明细
 */
function setHistoryLoan(){
	_C.deleteAllRow(1);
	let psncode = jQuery("#"+financePsnCode).val();
	let unitcode = jQuery("#"+financeUnitCode).val();
	if(
		_F.null2String(psncode) != '' &&
		_F.null2String(unitcode) != '' 
	){
		let url = "/NcApi.getHistoryLoan.app";
		let data = {
			psncode : psncode,
			unitcode : unitcode
		};
		let result = _C.ajaxJson(url,data);
		if(result){
			for(let i=0;i<result.length;i++){
				let e = result[i];
				_C.addRow(1,function(n){
					_C.vt(loanPk+"_"+n,e.pk_jkbx);								//NC借款单主键
					_C.vt(loanCode+"_"+n,e.billno);								//借款单号
					_C.vt(loanDate+"_"+n,e.billdate);							//借款日期
					_C.vt(loanPsn+"_"+n,e.jkbxr);								//借款人
					_C.vt(loanPsnCom+"_"+n,e.unitname);							//借款公司
					_C.vt(loanPsnDept+"_"+n,e.deptname);						//借款部门
					_C.vt(loanNote+"_"+n,e.billtype);							//借款事由
					_C.vt(loanAmount+"_"+n,_F.getNum(e.ybje).toFixed(2));		//借款金额
					_C.vt(loanBalance+"_"+n,_F.getNum(e.ybye).toFixed(2));		//借款余额
					_C.v(reversalAmount+"_"+n,"0.00");							//冲销金额
					_C.bindChange(reversalAmount+"_"+n,function(){
						var loanBalanceId = loanBalance+"_"+n;
						var reversalAmountId = reversalAmount+"_"+n;
						checkReversalAmounts(loanBalanceId,reversalAmountId);
					});
				});
			}
		}
	}
}

/**
 * 冲销金额录入控制
 * @param loanBalanceId
 * @param reversalAmountId
 */
function checkReversalAmounts(loanBalanceId,reversalAmountId){
	let reversalAmountVal = _F.getNum(jQuery("#"+reversalAmountId).val());	//冲销金额
	let loanBalanceVal = _F.getNum(jQuery("#"+loanBalanceId).val());			//借款余额
	if(reversalAmountVal>loanBalanceVal){
		window.top.Dialog.alert("冲销金额不能大于借款余额!");
		_C.v(reversalAmountId,"0.00");	
	}
}
